import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/page/train/train_user_check_recoder_my_remark_add_page.dart';
import 'package:get/get.dart';

import '../../bean/bean_train.dart';
import '../../utils/toast_utils.dart';
import '../common/common_loading_dialog.dart';
import '../common/common_nodata_widget.dart';
import 'controller/train_synthesize_check_report_controller.dart';

///教师的培训
class TrainSynthesizeCheckReportDetails extends StatefulWidget {
  final String tag;

  final int trainId;

  const TrainSynthesizeCheckReportDetails(
      {Key key, @required this.trainId, @required this.tag})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _TrainSynthesizeCheckReportDetailsState();
  }
}

class _TrainSynthesizeCheckReportDetailsState
    extends State<TrainSynthesizeCheckReportDetails>
    with SingleTickerProviderStateMixin {
  TrainSynthesizeCheckReportController _checkRecoderController;

  @override
  void initState() {
    super.initState();
    _checkRecoderController = Get.put(
      TrainSynthesizeCheckReportController(widget.trainId),
      tag: widget.tag,
    );

    Future.delayed(Duration.zero, () async {
      await _checkRecoderController.requestUserCheckRecoderFunction();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("综合考评报告详情"),
      ),
      body: GetBuilder(
        id: "root",
        tag: widget.tag,
        builder: (TrainSynthesizeCheckReportController controller) {
          return buildAnimatedSwitcher(context);
        },
      ),
    );
  }

  AnimatedSwitcher buildAnimatedSwitcher(BuildContext context) {
    return AnimatedSwitcher(

        //动画执行切换时间
        duration: const Duration(milliseconds: 200),
        //动画构建器 构建指定动画类型
        transitionBuilder: (Widget child, Animation<double> animation) {
          //执行缩放动画
          return FadeTransition(child: child, opacity: animation);
        },
        //执行动画的子 Widget
        //只有子 Widget 被切换时才会触发动画
        child: buildMyScoreHisIntegralWidget());
  }

  buildMyScoreHisIntegralWidget() {
    bool _isLoading = _checkRecoderController.getIsLoading();
    if (_isLoading) {
      return Center(
        child: LoadingWidget(),
      );
    }
    TrainSynthesizeCheckReportEntity _checkRecoderEntity =
        _checkRecoderController.getTrainUserCheckRecoderEntity();
    if (_checkRecoderEntity == null) {
      TrainBean trainBean = _checkRecoderController.getTrainBean();
      if (trainBean == null || trainBean.trainStatus != 5) {
        return Center(child: NoDataWidget(
          clickCallBack: () {
            _checkRecoderController.requestUserCheckRecoderFunction(
                click: true);
          },
        ));
      }
      return Center(
        child: ElevatedButton(
          onPressed: () {
            _checkRecoderController.createReportRequest();
          },
          child: Text("点击生成报告"),
        ),
      );
    }
    if (_checkRecoderEntity == null) {
      return Center(
        child: NoDataWidget(
          text: "暂无考核结果",
          clickCallBack: () {
            _checkRecoderController.requestUserCheckRecoderFunction(
                click: true);
          },
        ),
      );
    }

    return RefreshIndicator(
      onRefresh: () async {
        await _checkRecoderController.requestUserCheckRecoderFunction();
        ToastUtils.showToast("已刷新");
        return true;
      },
      child: SingleChildScrollView(
        child: buildListView(),
      ),
    );
  }

  buildListView() {
    TrainSynthesizeCheckReportEntity _checkRecoderEntity =
        _checkRecoderController.getTrainUserCheckRecoderEntity();

    int showUpdateMyRemark = _checkRecoderEntity.showUpdateMyRemark;
    return Column(
      children: [
        Container(
          color: Colors.white,
          width: double.infinity,
          margin: EdgeInsets.only(bottom: 12),
          child: Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [],
              )),
        ),
        buildCardWidget(
            headerTitle: "培训班的基本情况",
            onPressed: () async {
              await Get.to(
                () => TrainUserCheckRecoderAddPage(
                  trainUserCheckEnum: TrainUserCheckEnum.trainInfo,
                  recoderId: _checkRecoderEntity.id,
                  remark: _checkRecoderEntity.trainInfo,
                ),
              );
              _checkRecoderController.requestUserCheckRecoderFunction(
                  click: true);
            },
            content: "${_checkRecoderEntity.trainInfo}"),
        buildCardWidget(
            headerTitle: "班培训学时的落实执行情况",
            onPressed: () async {
              await Get.to(
                () => TrainUserCheckRecoderAddPage(
                  trainUserCheckEnum: TrainUserCheckEnum.trainPeriodInfo,
                  recoderId: _checkRecoderEntity.id,
                  remark: _checkRecoderEntity.trainPeriodInfo,
                ),
              );
              _checkRecoderController.requestUserCheckRecoderFunction(
                  click: true);
            },
            content: "${_checkRecoderEntity.trainPeriodInfo}"),
        buildCardWidget(
            headerTitle: "班培训课程的落实执行情况",
            onPressed: () async {
              await Get.to(
                () => TrainUserCheckRecoderAddPage(
                  trainUserCheckEnum: TrainUserCheckEnum.trainCourseInfo,
                  recoderId: _checkRecoderEntity.id,
                  remark: _checkRecoderEntity.trainCourseInfo,
                ),
              );
              _checkRecoderController.requestUserCheckRecoderFunction(
                  click: true);
            },
            content: "${_checkRecoderEntity.trainCourseInfo ?? "无"}"),
        buildCardWidget(
            headerTitle: "培训班取得成效",
            onPressed: () async {
              await Get.to(
                () => TrainUserCheckRecoderAddPage(
                  trainUserCheckEnum: TrainUserCheckEnum.trainResultInfo,
                  recoderId: _checkRecoderEntity.id,
                  remark: _checkRecoderEntity.trainResultInfo,
                ),
              );
              _checkRecoderController.requestUserCheckRecoderFunction(
                  click: true);
            },
            content: _checkRecoderEntity.trainResultInfo),
        buildCardWidget(
            headerTitle: "该期培训班存在的问题",
            onPressed: () async {
              await Get.to(
                () => TrainUserCheckRecoderAddPage(
                  trainUserCheckEnum: TrainUserCheckEnum.trainProblem,
                  recoderId: _checkRecoderEntity.id,
                  remark: _checkRecoderEntity.trainProblem,
                ),
              );
              _checkRecoderController.requestUserCheckRecoderFunction(
                  click: true);
            },
            content: "${_checkRecoderEntity.trainProblem ?? "无"}"),
        buildCardWidget(
            headerTitle: "下一步搞好培训班采取的措施",
            onPressed: () async {
              await Get.to(
                () => TrainUserCheckRecoderAddPage(
                  trainUserCheckEnum: TrainUserCheckEnum.trainPlan,
                  recoderId: _checkRecoderEntity.id,
                  remark: _checkRecoderEntity.trainPlan,
                ),
              );
              _checkRecoderController.requestUserCheckRecoderFunction(
                  click: true);
            },
            content: _checkRecoderEntity.trainPlan),
      ],
    );
  }

  buildCardWidget(
      {int showUpdateMyRemark = 0,
      String headerTitle,
      VoidCallback onPressed,
      String content}) {
    return Container(
      color: Colors.white,
      width: double.infinity,
      margin: EdgeInsets.only(bottom: 12),
      child: Padding(
          padding: EdgeInsets.all(10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: [
              Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    headerTitle,
                    style: TextStyle(
                        fontWeight: FontWeight.w500, color: Colors.black),
                  ),
                  Expanded(
                    child: SizedBox(width: 10),
                  ),
                  showUpdateMyRemark == 0
                      ? TextButton(
                          onPressed: () async {
                            if (onPressed != null) {
                              onPressed();
                            }
                          },
                          child: Text("编辑"))
                      : SizedBox()
                ],
              ),
              SizedBox(height: 4),
              Text(
                content ?? "无",
                style: TextStyle(
                    fontWeight: FontWeight.w400, color: Color(0xff666666)),
              )
            ],
          )),
    );
  }
}
